using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.ComponentModel;
using EcsSiteDSTableAdapters;

namespace SiteECSWeb
{
    [DataObject]
    public class PersonBLL
    {
        private PersonTableAdapter _PersonAdapter = null;

        protected PersonTableAdapter PersonAdapter
        {
            get
            {
                if (_PersonAdapter == null)
                {
                    _PersonAdapter = new PersonTableAdapter();
                }
                return _PersonAdapter;
            }
        } 

        #region SelectPerosn
        [DataObjectMethodAttribute(DataObjectMethodType.Select, true)]
        public EcsSiteDS.PersonDataTable GetPersonData()
        {
            return PersonAdapter.GetPersonRowRankData();   
        }


        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public EcsSiteDS.PersonDataTable GetPersonDataByPersonID(int Persn_id)
        {
            if (Persn_id <= 0)
            {
                return PersonAdapter.GetPersonRowRankData();
            }
            else
            {
                return PersonAdapter.GetPersonDataByPersonId(Persn_id);
            }
        }
        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public EcsSiteDS.PersonDataTable GetPersonDataByWcompanyID(int Wcomp_id)
        {
            return PersonAdapter.GetPersonDataByWcompId(Wcomp_id);
        }
        [DataObjectMethodAttribute(DataObjectMethodType.Select, false)]
        public EcsSiteDS.PersonDataTable GetPersonByLCompanyID(int LCompanyID)
        {
            return PersonAdapter.GetPersonDataByLcompId(LCompanyID);
        }

        #endregion

        #region InsertPerson

        [DataObjectMethodAttribute(DataObjectMethodType.Insert, true)]
        public int AddPerson
            (string Persn_namear, string Persn_namen, string Persn_phone, string Persn_fax, 
            string Persn_mob, string Persn_email, int? Wcomp_id, int? Comp_id, int? EmpTitle_id)
        {
            EcsSiteDS.PersonDataTable PersonTable = new EcsSiteDS.PersonDataTable();
            EcsSiteDS.PersonRow Person_Row = PersonTable.NewPersonRow();
            
            if (string.IsNullOrEmpty(Persn_namear )) Person_Row.SetPersn_namearNull(); else Person_Row.Persn_namear = Persn_namear;
            if (string.IsNullOrEmpty(Persn_namen)) Person_Row.SetPersn_namenNull(); else Person_Row.Persn_namen = Persn_namen;
            if (string.IsNullOrEmpty(Persn_phone)) Person_Row.SetPersn_phoneNull(); else Person_Row.Persn_phone = Persn_phone;
            if (string.IsNullOrEmpty(Persn_fax)) Person_Row.SetPersn_faxNull(); else Person_Row.Persn_fax = Persn_fax;
            if (string.IsNullOrEmpty(Persn_mob)) Person_Row.SetPersn_mobNull(); else Person_Row.Persn_mob = Persn_mob;
            if (string.IsNullOrEmpty(Persn_email)) Person_Row.SetPersn_emailNull(); else Person_Row.Persn_email = Persn_email;
            if (Wcomp_id == null) Person_Row.SetWcomp_idNull(); else Person_Row.Wcomp_id = Wcomp_id.Value;
            if (Comp_id == null) Person_Row.SetComp_idNull(); else Person_Row.Comp_id = Comp_id.Value;

            PersonTable.AddPersonRow(Person_Row);
            int rowsAffected = PersonAdapter.Update(PersonTable);
            return (rowsAffected == 1) ? Person_Row.Persn_id : -1;
        }


        #endregion

        #region UpdatePerson

        [DataObjectMethodAttribute(DataObjectMethodType.Update, true)]
        public bool UpdatePerson
            (string Persn_namear, string Persn_namen, string Persn_phone, string Persn_fax,
            string Persn_mob, string Persn_email, int? Wcomp_id, int? Comp_id, int? EmpTitle_id, int Persn_id)
        {
            EcsSiteDS.PersonDataTable PersonTable = PersonAdapter.GetPersonDataByPersonId(Persn_id);
            if (PersonTable.Count==0)
            {
                return false;
            }

            EcsSiteDS.PersonRow Person_Row = PersonTable[0];
            if (string.IsNullOrEmpty(Persn_namear )) Person_Row.SetPersn_namearNull(); else Person_Row.Persn_namear = Persn_namear;
            if (string.IsNullOrEmpty(Persn_namen)) Person_Row.SetPersn_namenNull(); else Person_Row.Persn_namen = Persn_namen;
            if (string.IsNullOrEmpty(Persn_phone)) Person_Row.SetPersn_phoneNull(); else Person_Row.Persn_phone = Persn_phone;
            if (string.IsNullOrEmpty(Persn_fax)) Person_Row.SetPersn_faxNull(); else Person_Row.Persn_fax = Persn_fax;
            if (string.IsNullOrEmpty(Persn_mob)) Person_Row.SetPersn_mobNull(); else Person_Row.Persn_mob = Persn_mob;
            if (string.IsNullOrEmpty(Persn_email)) Person_Row.SetPersn_emailNull(); else Person_Row.Persn_email = Persn_email;
            if (Wcomp_id == null) Person_Row.SetWcomp_idNull(); else Person_Row.Wcomp_id = Wcomp_id.Value;
            if (Comp_id == null) Person_Row.SetComp_idNull(); else Person_Row.Comp_id = Comp_id.Value;

            //Person_Row.Persn_id = Persn_id;
            PersonTable.AddPersonRow(Person_Row);

            int rowsAffected = PersonAdapter.Update(PersonTable);
            return rowsAffected == 1;
        }


        #endregion

        #region DeletePerson
        
        [DataObjectMethodAttribute(DataObjectMethodType.Delete, true)]
        public bool DeletePerson(int Persn_id)
        {
            int rowsAffected = PersonAdapter.Delete(Persn_id);
            return rowsAffected == 1;
        }


        #endregion

    }
}
